"""监控和审计相关模型."""

from datetime import datetime
from typing import Optional

from sqlalchemy import (
    DateTime,
    ForeignKey,
    Index,
    Integer,
    JSON,
    String,
    Text,
    func,
)
from sqlalchemy.orm import Mapped, mapped_column

from .base import Base


class Metric(Base):
    """性能指标模型."""
    
    __tablename__ = "metric"
    
    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    
    # 指标信息
    name: Mapped[str] = mapped_column(String(255), nullable=False)
    value: Mapped[float] = mapped_column(nullable=False)
    unit: Mapped[str] = mapped_column(String(50))
    
    # 标签
    labels: Mapped[Optional[dict]] = mapped_column(JSON)
    
    # 时间戳
    timestamp: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), 
        server_default=func.now(),
        nullable=False
    )
    
    # 约束
    __table_args__ = (
        Index("idx_metric_name", "name"),
        Index("idx_metric_timestamp", "timestamp"),
    )


class Alert(Base):
    """告警模型."""
    
    __tablename__ = "alert"
    
    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    
    # 告警信息
    name: Mapped[str] = mapped_column(String(255), nullable=False)
    severity: Mapped[str] = mapped_column(
        String(20), 
        nullable=False
    )  # info, warning, error, critical
    message: Mapped[str] = mapped_column(Text, nullable=False)
    
    # 状态
    status: Mapped[str] = mapped_column(
        String(20), 
        default="active", 
        nullable=False
    )  # active, acknowledged, resolved
    
    # 详细信息
    details: Mapped[Optional[dict]] = mapped_column(JSON)
    
    # 时间戳
    created_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), 
        server_default=func.now(),
        nullable=False
    )
    resolved_at: Mapped[Optional[datetime]] = mapped_column(
        DateTime(timezone=True), 
        nullable=True
    )
    
    # 约束
    __table_args__ = (
        Index("idx_alert_severity", "severity"),
        Index("idx_alert_status", "status"),
        Index("idx_alert_created", "created_at"),
    )


class AuditLog(Base):
    """审计日志模型."""
    
    __tablename__ = "audit_log"
    
    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    
    # 操作信息
    action: Mapped[str] = mapped_column(String(50), nullable=False)  # CREATE, UPDATE, DELETE
    resource_type: Mapped[str] = mapped_column(String(50), nullable=False)
    resource_id: Mapped[str] = mapped_column(String(255), nullable=False)
    
    # 变更详情
    old_values: Mapped[Optional[dict]] = mapped_column(JSON)
    new_values: Mapped[Optional[dict]] = mapped_column(JSON)
    
    # 用户信息
    user_id: Mapped[Optional[str]] = mapped_column(String(255))
    user_name: Mapped[Optional[str]] = mapped_column(String(255))
    
    # 请求信息
    ip_address: Mapped[Optional[str]] = mapped_column(String(39))
    user_agent: Mapped[Optional[str]] = mapped_column(Text)
    
    # 时间戳
    timestamp: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), 
        server_default=func.now(),
        nullable=False
    )
    
    # 约束
    __table_args__ = (
        Index("idx_audit_resource", "resource_type", "resource_id"),
        Index("idx_audit_user", "user_id"),
        Index("idx_audit_timestamp", "timestamp"),
    )